package cn.stylefeng.guns.modular.info;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.utils.StringUtil;
import cn.stylefeng.guns.modular.info.entity.League;
import cn.stylefeng.guns.modular.info.service.GameService;
import cn.stylefeng.guns.modular.info.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: guns-vip
 * @description: 资料库
 * @author: xiesm
 * @create: 2019-12-30 16:43
 **/
@Controller
@RequestMapping("/info")
public class InfoController {

    private String PREFIX = "/info/%s.html";
    @Autowired
    private InfoService infoService;
    @Autowired
    private GameService gameService;

    /**
     * @Description: 跳转到资料库主页面
     * @Author: xiesm
     * @Date: 2019/12/31 9:20
     */
    @RequestMapping("")
    public String index() {
        return String.format(PREFIX, "info");
    }

    /**
     * @Description: 跳转到资料库查看
     * @Author: xiesm
     * @Date: 2019/12/31 9:20
     */
    @RequestMapping("/view")
    public String view(String fkLeagueId, HttpServletRequest req, String currentRun, String teamNum) {
        req.setAttribute("fkLeagueId", fkLeagueId);
        req.setAttribute("currentRun", currentRun);
        req.setAttribute("teamNum", teamNum);
        req.setAttribute("seasons", gameService.getYear(fkLeagueId));
        req.setAttribute("nums", gameService.getRunCount(fkLeagueId));
        return String.format(PREFIX, "info_view");
    }

    /**
     * @Description: 查询列表
     * @Author: xiesm
     * @Date: 2019/12/30 17:56
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(@RequestParam(value = "condition", required = false) String condition) {
        League league = new League();
        league.setName(condition);
        return LayuiPageFactory.createPageInfo(this.infoService.findLeagueList(league));
    }


    /**
     * @Description: 查询列表
     * @Author: xiesm
     * @Date: 2019/12/30 17:56
     */
    @ResponseBody
    @RequestMapping("/gameList")
    public LayuiPageInfo gameList(@RequestParam(value = "fkLeagueId", required = false) String fkLeagueId,
                                  @RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "ownGoal", required = false) String ownGoal,
                                  @RequestParam(value = "pointGoal", required = false) String pointGoal,
                                  @RequestParam(value = "redCard", required = false) String redCard,
                                  @RequestParam(value = "year", required = false) String year
    ) {
        Map<String, String> param = new HashMap<>();
        param.put("name", StringUtil.isNotNull(name) ? name : null);
        param.put("ownGoal", StringUtil.isNotNull(ownGoal) ? ownGoal : null);
        param.put("pointGoal", StringUtil.isNotNull(pointGoal) ? pointGoal : null);
        param.put("redCard", StringUtil.isNotNull(redCard) ? redCard : null);
        param.put("year", StringUtil.isNotNull(year) ? year : null);
        param.put("fkLeagueId", fkLeagueId);
        return this.gameService.findGameList(param);
    }

}
